Python考试

您所在的位置:网站首页 python 列表套列表 Python考试

Python考试

2023-07-12 18:48| 来源: 网络整理| 查看: 265

1.正则表达式

import re aa="""柳宗元(773年—819年11月28日),字子厚,汉族,祖籍河东郡(今山西省运城市永济、芮城一带)人。 代表作有《溪居》《江雪》《渔翁》。""" print(re.findall("《.+?》",aa)) #re.findall("《\w+》",aa),findall函数————查找 dd="""一氧化碳,一种碳氧化合物,通常状况下为是无色、无臭、无味的气体。物理性质上,一氧化碳的熔点为-205℃,沸点为-191.5℃,难溶于水。化学性质上,一氧化碳既有还原性,又有氧化性,能发生氧化反应(燃烧反应)、歧化反应等;同时具有毒性,较高浓度时能使人出现不同程度中毒症状,危害人体的脑、心、肝、肾、肺及其他组织,甚至电击样死亡,人吸入最低致死浓度为5000ppm(5分钟)。工业上,一氧化碳是一碳化学的基础,可由焦炭氧气法等方法制得,主要用于生产甲醇和光气以及有机合成等。""" print(re.sub("一氧化碳","CO",dd)) #sub函数————替换(“被替换词”,“替换词”,对象)

2.xpath爬取数据框架

第二段代码包含split()——分隔函数,strip()——去空格函数的应用

去掉数字后的文字的两种方法

    ①正则表达式re.sub()     # aa=myli.xpath(".//div[@class='star']//span[4]/text()")[0]     # price=re.sub("\D","",aa)     ②字符串的   .replace()方法     price = myli.xpath(".//div[@class='star']//span[4]/text()")[0].replace("人评价",'')

import requests import re from lxml import etree url="https://www.80wx.org/top/lastupdate/1.html" headers={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0"} response=requests.get(url=url,headers=headers).text #decode(request.get().content)解决乱码 mytree=etree.HTML(response) myresult=mytree.xpath("""//*[@id="list_art_2013"]/div""") resultlist=[] for myli in myresult: name=myli.xpath("""//div[@class="book_bg"]/a/text()""")[0] author=myli.xpath("""//div[@class="book_cont"]/span/a/text()""")[0] aa = myli.xpath("""//*[@id="list_art_2013"]/div[1]/div[3]/span/text()[3]""")[0].strip() size=re.sub("KB","",aa) bb = myli.xpath("""//*[@id="list_art_2013"]/div[1]/div[3]/span/text()[4]""")[0].strip() number = re.sub("次", "", bb) mylist=[name,author,size,number] resultlist.append(mylist) print(resultlist) import requests import re from lxml import etree url="https://www.meishij.net/chufang/diy/" headers={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0"} html=requests.get(url=url,headers=headers).text aa=etree.HTML(html) nn=aa.xpath('//div[@class="listtyle1_list clearfix"]/div') #print(nn) totalist=[] for ii in nn: title=ii.xpath('.//strong//text()')[0].strip() zuozhe=ii.xpath('.//em//text()')[0].strip() buzou=ii.xpath('.//li[1]//text()')[0].split(" / ") step=buzou[0].strip() shijian=buzou[1].strip() buzou2 = ii.xpath('.//li[2]//text()')[0].split(" / ") zuofa = buzou2[0].strip() taste = buzou2[1].strip() mylist=[title,zuozhe,step,shijian,zuofa,taste] totalist.append(mylist) print(totalist)

3.数据存储至csv代码结构

数据存储到csv中注意一定要列表套字典,如果是列表套列表的话会出现存储为空的情况

import csv tou = list(totalist[0].keys()) #写头的通用代码,也可以自己写头 with open("电影2.csv", "w", newline="", encoding="utf-8") as f: ff = csv.DictWriter(f, tou) ff.writeheader() ff.writerows(totalist)

如果不用字典

import csv with open("mydata.csv","w") as f: ff=csv.writer(f) ff.writerow(["小说名","作者","大小","总下载次数"]) ff.writerows(resultlist)

4.图片存储结构

import requests from lxml import etree url="https://www.80wx.org/top/lastupdate/1.html" headers={"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0"} response=requests.get(url=url,headers=headers).text #decode(request.get().content)解决乱码 mytree=etree.HTML(response) myresult=mytree.xpath('//div[@class="list_box"]/div') for myli in myresult: author=myli.xpath("""//*[@id="list_art_2013"]/div[1]/div[1]/div[1]/a/text()""")[0] pic=myli.xpath("""//*[@id="list_art_2013"]/div[2]/a/img/@src""")[0] # print(title,pic) tupian=requests.get(url=url,headers=headers).content with open("{}.jpg".format(author),"wb") as f: f.write(tupian)

5.数据可视化(饼图)代码结构

import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.figure(figsize=(20,8),dpi=100) plt.rcParams['font.sans-serif']=['SimHei'] score=[98,60,74,85,30,10] fenli=[0,0,0,0,0,0.5] names=['张三','李四','赵武','张丽','王鹏','孙丽丽'] plt.pie(score,explode=fenli,labels=names,autopct='%.2f%%',shadow=True) plt.title("学生成绩",fontsize=15) plt.legend(loc=0) plt.show()



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3